package DD2;

class Window1 implements Runnable{
	private static Object obj = new Object();
	private int money = 110;
	int TakeMoney = 0;

	public void run() {
		while (true) {
			synchronized (obj) {// Use "synchronized" to lock the process {

				if (money >=20) {

					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					money -= 20;
					System.out.println(Thread.currentThread().getName() + " take money,money left " + money);
					TakeMoney += 20;
				} else {
					break;
				}
			}
		}
		System.out.println("totally take "+TakeMoney);
	}
}

public class WindowTest1 {
	public static void main(String[] args) {
		Window1 w = new Window1();

		Thread t1 = new Thread(w);
		Thread t2 = new Thread(w);
		Thread t3 = new Thread(w);

		t1.setName("Person1");
		t2.setName("Person2");
		t3.setName("Person3");

		t1.start();
		t2.start();
		t3.start();
	}

}


class Dog{

}